package com.ltmall.api.comm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    public static String pattern[] = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss"};

    public void test() {
        String st = "09:25";
        String et = "10:35";
        boolean tag = inTime(st, et);
        if (tag) {
            System.out.println("----在时间段之间---");
        } else {
            System.out.println("----不在时间段之间---");
        }

    }

    /**
     * 获取指定时间毫秒数
     *
     * @param time time
     * @param temp temp
     */
    public static long getLongTime(String time, int temp) {
        long l = 0;
        String p = pattern[temp];
        SimpleDateFormat format = new SimpleDateFormat(p);
        try {
            Date date = format.parse(time);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            // 获取时间的毫秒数
            l = c.getTimeInMillis();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return l;
    }

    // 获取指定时间毫秒数
    public static String getStringTime(long time, int temp) {
//		String pattern = "yyyy-MM-dd HH:mm";
        if (time == 0) {
            return "0";
        }
        String p = pattern[temp];
        SimpleDateFormat format = new SimpleDateFormat(p);
        String sTime = "";
        try {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(time);
            c.getTime();
            sTime = format.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sTime;
    }


    //当前时间是否在小时段以内
    public static boolean inTime(String startTimeS, String endTimeS) {
        long now = System.currentTimeMillis();

        long startTime = date(startTimeS);
        long endTime = date(endTimeS);
        if (now >= startTime && now <= endTime) {
            return true;
        }
        return false;

    }

    ;


    /*
     * time : 09:25
     */
    public static long date(String t) {
        String[] time = t.split(":");
        String hour = time[0];
        String minute = time[1];

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        ca.set(Calendar.MINUTE, Integer.parseInt(minute));
        return ca.getTimeInMillis();

    }

    public void testHalfYear() {
        long startTime = 1468836612967L;
        long endTime = getEndTime(startTime, 2, 6);
        System.out.println(endTime);

    }

    // 获取半年的结束时间
    public static long getEndTime(Long startTime, int type, int number) {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(startTime);
        calendar.setTime(date);

        // 年份
        if (type == 1) {
            calendar.add(Calendar.YEAR, number);
        }
        //月份
        else if (type == 2) {
            calendar.add(Calendar.MONTH, number);
        }


        return calendar.getTimeInMillis();
    }


}
